package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.EmailQueueVo;
import org.dromara.system.domain.bo.EmailQueueBo;
import org.dromara.system.service.IEmailQueueService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 邮件队列
 * 前端访问路由地址为:/system/EmailQueue
 *
 * @author Gavino
 * @date 2024-11-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/EmailQueue")
public class EmailQueueController extends BaseController {

    private final IEmailQueueService emailQueueService;

    /**
     * 查询邮件队列列表
     */
    @SaCheckPermission("system:EmailQueue:list")
    @GetMapping("/list")
    public TableDataInfo<EmailQueueVo> list(EmailQueueBo bo, PageQuery pageQuery) {
        return emailQueueService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件队列列表
     */
    @SaCheckPermission("system:EmailQueue:export")
    @Log(title = "邮件队列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmailQueueBo bo, HttpServletResponse response) {
        List<EmailQueueVo> list = emailQueueService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件队列", EmailQueueVo.class, response);
    }

    /**
     * 获取邮件队列详细信息
     *
     * @param queueId 主键
     */
    @SaCheckPermission("system:EmailQueue:query")
    @GetMapping("/{queueId}")
    public R<EmailQueueVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long queueId) {
        return R.ok(emailQueueService.queryById(queueId));
    }

    /**
     * 新增邮件队列
     */
    @SaCheckPermission("system:EmailQueue:add")
    @Log(title = "邮件队列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmailQueueBo bo) {
        return toAjax(emailQueueService.insertByBo(bo));
    }

    /**
     * 发送邮件队列
     */
    @SaCheckPermission("system:EmailQueue:send")
    @Log(title = "邮件队列", businessType = BusinessType.OTHER)
    @GetMapping(value = "/send/{queueId}/{status}")
    public R<?> send(@PathVariable("queueId") Long queueId, @PathVariable("status") Integer status) {
        if (status != 4 && status != 1) {
            return R.fail("参数有误");
        }
        return emailQueueService.sendEmailQueue(queueId, status, LoginHelper.getLoginUser());
    }

    /**
     * 所有失败邮件队列重发
     */
    @SaCheckPermission("system:EmailQueue:send")
    @Log(title = "所有失败邮件队列重发", businessType = BusinessType.OTHER)
    @GetMapping(value = "/reSendAll")
    public R<?> reSendAll() {
        return emailQueueService.reSendAll();
    }

    /**
     * 修改邮件队列
     */
    @SaCheckPermission("system:EmailQueue:edit")
    @Log(title = "邮件队列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmailQueueBo bo) {
        return toAjax(emailQueueService.updateByBo(bo));
    }

    /**
     * 删除邮件队列
     *
     * @param queueIds 主键串
     */
    @SaCheckPermission("system:EmailQueue:remove")
    @Log(title = "邮件队列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{queueIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] queueIds) {
        return toAjax(emailQueueService.deleteWithValidByIds(List.of(queueIds), true));
    }
}
